package cn.gdcp.myapplication3;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainAdapter extends BaseAdapter {
    private ArrayList<Student> studentArrayList;
    private Context context;


    public MainAdapter(Context context, ArrayList<Student> studentArrayList){
        this.context=context;
        this.studentArrayList=studentArrayList;
    }
    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    int count=0;
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Log.e("MainAdapter", "getview" + i + "count=" + count);

        if(view == null) {
            count++;
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            view = layoutInflater.inflate(R.layout.my_list_item,
                    viewGroup, false);

            StuViewHolder holder = new StuViewHolder();
            holder.headImg = view.findViewById(R.id.stu_imge);
            holder.nameView = view.findViewById(R.id.stu_name);
            holder.ageView = view.findViewById(R.id.stu_age);

            view.setTag(holder);
        }

        StuViewHolder stuViewHolder=(StuViewHolder)view.getTag();
        Student stu = studentArrayList.get(i);
        stuViewHolder.headImg.setImageResource(studentArrayList.get(i).getImgid());
        stuViewHolder.nameView.setText(studentArrayList.get(i).getName());
        stuViewHolder.ageView.setText(String.valueOf(studentArrayList.get(i).getAge()));

        return view;
    }
}
